﻿/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A
 * copy of the license can be found at http://go.microsoft.com/fwlink/?LinkID=131993. If
 * you cannot locate the Microsoft Public License, please send an email to
 * mixon@microsoft.com. By using this source code in any fashion, you are agreeing to
 * be bound by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MIXOnline.Descry
{
    public enum Unit { FifteenMinute, Hour, HalfDay, Day, Week, Month, Year };

    public class View : INotifyPropertyChanged
    {
        private Unit _unit;
        public Unit Unit
        {
            get { return _unit; }
            set { _unit = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private TimeSpan _timeSpan;
        public TimeSpan TimeSpan
        {
            get { return _timeSpan; }
            set { _timeSpan = value; }
        }

        private int _columns;

        public int Intervals
        {
            get { return _columns; }
            set { _columns = value; }
        }

        private bool _isChecked;

        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                NotifyPropertyChanged("IsChecked");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }

    public class Views : ObservableCollection<View>
    {
        public Views()
        {
            //this.Add(new View() { Name = "FifteenMinute", Unit = Unit.FifteenMinute, TimeSpan = new TimeSpan(0, 0, 15, 0),  Intervals = 15 });
            this.Add(new View() { Name = "Hour",      Unit = Unit.Hour,      TimeSpan = new TimeSpan(0, 1, 0, 0),   Intervals = 12 });
            this.Add(new View() { Name = "Day",       Unit = Unit.Day,       TimeSpan = new TimeSpan(1, 0, 0, 0),   Intervals = 12, IsChecked = true});
            //this.Add(new View() { Name = "HalfDay",   Unit = Unit.HalfDay,   TimeSpan = new TimeSpan(0, 12, 0, 0),  Intervals = 12 });
            this.Add(new View() { Name = "Week",      Unit = Unit.Week,      TimeSpan = new TimeSpan(7, 0, 0, 0),   Intervals = 7 }); // one per day
            this.Add(new View() { Name = "Month",     Unit = Unit.Month,     TimeSpan = new TimeSpan(31, 0, 0, 0),  Intervals = 31 }); // since they're 31 days we'll round up so index works better.
            this.Add(new View() { Name = "Year",      Unit = Unit.Year,      TimeSpan = new TimeSpan(366, 0, 0, 0), Intervals = 12 });
        }

        public static View GetViewForUnits(Unit unit)
        {
            foreach (View view in new Views())
            {
                if (view.Unit == unit)
                    return view;
            }
            return null;
        }

        public View GetView(string viewname)
        {
            foreach (View view in this)
            {
                if (view.Name == viewname)
                    return view;
            }
            return null;
        }

        private int _selectedIndex = 0;

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; }
        }

        static public DateTime GetStartingTimeForView(View view)
        {
            DateTime result = DateTime.Now;

            DateTime now = DateTime.Now;

            switch (view.Unit)
            {
                case Unit.FifteenMinute:
                    result = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0); // TODO: starting time
                    break;

                case Unit.Day:
                    result = now.Date; // starting time is at beginning of day
                    break;
                case Unit.Hour:
                    result = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0); // starting time is at beginning of hour
                    break;
                case Unit.Month:
                    result = new DateTime(now.Year, now.Month, 1, 0, 0, 0); // starting time is at beginning of month
                    break;
                case Unit.Week:
                    result = new DateTime(now.Year, now.Month, now.Day) - new TimeSpan((int)now.DayOfWeek, 0, 0, 0); // figure out what day of the week it is and start the week on Sunday
                    break;
                case Unit.Year:
                    result = new DateTime(now.Year, 1, 1); // starting time is at beginning of year
                    break;
                //case Unit.HalfDay:
                    //break;
            }
            return result;
        }


    }
}
